package com.burgeon.backend.service.Impl;


import com.burgeon.backend.Rest.Tool.GetVIPTypeCount;
import com.burgeon.backend.Rest.User.GetRetailData;
import com.burgeon.backend.Rest.view.data.MainData;
import com.burgeon.backend.Rest.view.data.MainViewData;
import com.burgeon.backend.Rest.view.util.FirstMainView;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.viewMapper;
import com.burgeon.backend.service.ViewService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ViewServiceImpl implements ViewService {

    private final static Log logger = LogFactory.getLog(ViewService.class);

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private viewMapper viewMapper;

    @Override
    public Map getShopView(String account) {
        Date date = new Date();

        SimpleDateFormat osdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat gsdf = new SimpleDateFormat("yyyyMMdd");
        Map data = new HashMap();
        data.put("GoodsCount",viewMapper.GoodsCount(account));
        data.put("GoodsCountToday",viewMapper.GoodsCountToday(gsdf.format(date),account));
        data.put("OrderCountToday",viewMapper.OrderCountToday(osdf.format(date),account));
        data.put("OrderCountAwait",viewMapper.OrderCountAwait(account));
        data.put("OrderCountTake",viewMapper.OrderCountTake(account));

        return data;
    }

    @Override
    public Map GetRetailMap(String account) {
        Info info = GetInfo(account);
        GetRetailData getRetailData = new GetRetailData(info.getUrl(), info.getAccount(), info.getPassword());

        Map map = null;
        try {
            map = getRetailData.getData();
        } catch (Exception e) {
            logger.error(e.getMessage()+ Arrays.toString(e.getStackTrace()));
        }
        return map;
    }

    @Override
    public List GetVIPViewList(String account) {

        Info info = GetInfo(account);
        GetVIPTypeCount gc = new GetVIPTypeCount(info.getUrl(), info.getAccount(), info.getPassword());
        return gc.getData();
    }

    @Override
    public Map MainView(String account) {
        Info info = GetInfo(account);
        HashMap data = new HashMap();
        FirstMainView firstMainView = new FirstMainView(info.getUrl(), info.getAccount(), info.getPassword());
        MainData mdata = new MainData(info.getUrl(), info.getAccount(), info.getPassword());
        MainViewData wdata = new MainViewData(info.getUrl(), info.getAccount(), info.getPassword());

        try {
            firstMainView.getData();
            data.put("data",mdata.getData());
            data.put("view",wdata.getData());

        } catch (Exception e) {
            logger.error(e.getMessage());
            logger.error(e.getLocalizedMessage());
        }

        return data;
    }

    private Info GetInfo(String account) {
        return accountMapper.GetAccData(account);
    }


}
